function uploadPicture(imageUri) {
    function win(r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
        if(r.response == "MAX") {
            alert("图片太大超过4M，请剪裁小一点");
        }
    }

    function fail(error) {
        console.log("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
        //alert('图片上传失败。');
    }

    function progressFun(progressEvent) {
        var progress = progressEvent.loaded / progressEvent.total * 100;
        document.getElementById("progress").innerHTML = "上传进度:"+progress+"%100";
    }

    var options = new FileUploadOptions();
    options.fileKey = "picture";
    options.fileName = imageUri.substr(imageUri.lastIndexOf('/')+1);
    options.mimeType = "image/pjpeg";
    var headers={'oss-token':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGllbnRUeXBlIjoiYXBwIiwiaXNzIjoib3NzIiwidXNlclR5cGUiOjEsInNob3BJZCI6Ik1aIiwiZXhwIjoxNzU2MTA1NzY4LCJ1c2VySWQiOiJ5dHQifQ.VvjXxOxXtRHJQriCMBZJ_-szJJMwbe1qLbUD3kBmqCk'};
    options.headers = headers;
    var ft = new FileTransfer();
    ft.onprogress = progressFun;
    ft.upload(imageUri, "https://ceshi.tongecn.com/servlet/ImageTempUpload", win, fail, options);
    //ft.abort();
}

function upload() {
    var options = {
        // Some common settings are 20, 50, and 100
        quality: 50,
        destinationType: Camera.DestinationType.FILE_URI,
        // In this app, dynamically set the picture source, Camera or photo gallery
        sourceType: Camera.PictureSourceType.CAMERA,
        encodingType: Camera.EncodingType.JPEG,
        mediaType: Camera.MediaType.PICTURE,
        allowEdit: false,
        correctOrientation: true  //Corrects Android orientation quirks
    }

    navigator.camera.getPicture(function cameraSuccess(imageUri) {
        var elem = document.getElementById('imageFile');
        elem.src = imageUri;
        window.resolveLocalFileSystemURL(imageUri, function (fileEntry) {
            console.log(fileEntry.toURL());
            uploadPicture(fileEntry.toURL());
        });
    }, function cameraError(error) {
        console.debug("Unable to obtain picture: " + error, "app");
    }, options);

}

function download() {
    function onErrorReadFile(error) {

    }
    function displayImage(blob) {
        // Displays image if result is a valid DOM string for an image.
        var elem = document.getElementById('imageFile2');
        // Note: Use window.URL.revokeObjectURL when finished with image.
        elem.src = window.URL.createObjectURL(blob);
    }

    function readBinaryFile(fileEntry) {
        fileEntry.file(function (file) {
            var reader = new FileReader();
            reader.onloadend = function() {
                console.log("Successful file write: " + this.result);
                //displayFileData(fileEntry.fullPath + ": " + this.result);
                var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
                displayImage(blob);
                //displayImage2(this.result);
            };
            reader.readAsArrayBuffer(file);
            //reader.readAsDataURL(file);
            //reader.readAsBinaryString(file);
        }, onErrorReadFile);
    }

    function successFun(fileEntry) {
        console.log("download complete: " + fileEntry.toURL());
        readBinaryFile(fileEntry);
        //document.getElementById("imageFile2").src = "https://localhost/"+fileEntry.toURL();
    }

    function failFun(error) {
        console.log("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
        alert('图片下载失败。');
    }

    function progressFun(progressEvent) {
        var progress = progressEvent.loaded / progressEvent.total * 100;
        document.getElementById("progress").innerHTML = "下载进度:"+progress+"%100";
    }

    var uri = "https://cordova.apache.org/static/img/cordova_bot.png";
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
        var targetPath = dirEntry.toURL() + "中文.png";
        console.log("targetPath:" + targetPath);
        var fileTransfer = new FileTransfer();
        fileTransfer.onprogress = progressFun;
        fileTransfer.download(
            uri,
            targetPath,
            successFun,
            failFun,
            false,
            {}
        );
        //fileTransfer.abort();
    });
}
